package com.epam.cdp.jpa.converters;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import com.epam.cdp.jpa.dao.AuthorDAO;
import com.epam.cdp.jpa.model.Author;

@ManagedBean
@RequestScoped
public class AuthorConverter implements Converter {
    
    @EJB
    private AuthorDAO authorDAO;
    
    @Override
    public Object getAsObject(FacesContext context, UIComponent uiComponent, String stringRespresentation) {
	long parseLong = Long.parseLong(stringRespresentation);
	Object result = authorDAO.findById(parseLong);
	return result;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent uiComponent, Object object) {
	return String.valueOf(((Author)object).getId());
    }

}
